package com.stylefeng.guns.modular.system.service.impl;

import com.stylefeng.guns.common.constant.tips.ErrorTip;
import com.stylefeng.guns.common.constant.tips.SuccessTip;
import com.stylefeng.guns.common.constant.tips.Tip;
import com.stylefeng.guns.common.persistence.dao.JoinStepMapper;
import com.stylefeng.guns.common.persistence.dao.OrgInfoMapper;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.JoinStep;
import com.stylefeng.guns.common.persistence.model.OrgInfo;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.modular.system.dao.OrgInfoDao;
import com.stylefeng.guns.modular.system.dao.UserDao;
import com.stylefeng.guns.modular.system.service.OrgInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class OrgInfoServiceImpl implements OrgInfoService {

    private static final int WAITING = 97;//待审批
    private static final int PASS = 99;//通过
    private static final int REFUSE = 98;//拒绝

    @Resource
    JoinStepMapper joinStepMapper;
    @Resource
    OrgInfoMapper orgInfoMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    OrgInfoDao orgInfoDao;
    @Resource
    UserDao userDao;
    @Override
    public Tip pass(Integer id) {

        //如果通过会为机构创建一个admin账号
        User user = new User();
        //初始密码 111111
        user.setPassword("ecfadcde9305f8891bcfe5a1e28c253e");
        user.setSalt("8pgby");
        //账户号为org + 入驻id
        user.setAccount("org"+id);
        //2 表示代理商
        user.setRoleid("2");
        user.setDeptid(0);
        user.setName("代理商");
        //启用
        user.setStatus(1);
        user.setCreatetime(new Date());

        //返回新建的user id
        userDao.insertAndGetId(user);
        Integer userId = user.getId();
        if(userId<1){
            return new ErrorTip(500,"执行错误");
        }
        int i = edit(1,id,null,userId);
        if(i<1){
            return new ErrorTip(500,"执行错误");
        }
        orgInfoDao.updateAdminUserIdById(id,userId);
        return new SuccessTip();
    }

    @Override
    public Tip refuse(Integer id, String reason) {
        int i = edit(2,id,reason,0);
        if(i<1){
            return new ErrorTip(500,"执行错误");
        }
        return new SuccessTip();
    }

    /**
     * 1 表示用过类型 2 表示拒绝类型
     * @param type
     * @param id
     * @param reason
     * @return
     */
    private int edit(Integer type,Integer id,String reason,Integer userId){
        int i = 0;
        if(null == id){
            return i;
        }
        JoinStep joinStep = new JoinStep();
        joinStep.setId(id);
        switch (type){
            case 1:
                //流程节点
                joinStep.setReason(null);
                joinStep.setCurrentState(PASS);

                try {
                    //修改 机构 后台权限关联 userId
                    orgInfoDao.updateUserIdByJoinId(id,userId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 2:
                joinStep.setReason(reason);
                joinStep.setCurrentState(REFUSE);
                break;
            default:
                break;
        }
        try {
            i = joinStepMapper.updateById(joinStep);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

}
